<?php
if(!interface_exists('XSocket', false)){
	require(dirname(__FILE__).DIRECTORY_SEPARATOR.'XSocket.php');
}
abstract class XAbstractSocket implements XSocket{
    protected $created = false;
    protected $bound = false;
    protected $connected = false;
    protected $closed = false;
    protected $handler = null;
    protected $inetAddress = null;
    protected $port = null;
    protected $localAddress = null;
    protected $localPort = null;
    
    public function __construct($domain, $type, $protocol){
    	$this->handler = @socket_create($domain, $type, $protocol);
    	if($this->handler){
    		$this->created = true;
    	}
    }    	
	public function bind($localhost = 'localhost', $port = 0){
		if($this->bound){
			return true;
		}
		$this->bound = @socket_bind($this->handler, $localhost, $port);
		if($this->bound){
			@socket_getsockname($this->handler, $this->localAddress, $this->localPort);
		}
		return $this->bound;
	}
	public function close(){
		if($this->closed){
			return true;
		}
		$this->closed = @socket_close($this->handler);
		if($this->closed){
			$this->handler = null;
		}
		return $this->closed;
	}
	public function connect($host, $port){
		$this->connected = @socket_connect($this->handler, $host, $port);
		if($this->connected){
			@socket_getpeername($this->handler, $this->inetAddress, $this->port);
		}
		return $this->connected;
	}
	public function getInetAddress(){
		return $this->inetAddress;
	}	
	public function getLocalAddress(){
		return $this->localAddress;
	}
	public function getLocalPort(){
		return $this->localPort;
	}
	public function getPort(){
		return $this->port;
	}		
	public function handler(){
		return $this->handler;
	}
	public function isBound(){
		return $this->bound;
	}
	public function isClosed(){
		return $this->closed;
	}	
	public function isConnected(){
		return $this->connected;
	}
}
?>